<template>
  <page>
    <template v-slot:header>
      <h2>
        <span class="icon iconfont">&#xe607;</span>
        <span>{{ $t("configure.configure_group") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
      >
        <delete
          v-if="type === 'delete'"
          :content="delete_content"
          :handleCancel="handleClose"
          :handleOk="handleDeleteOk"
        >
        </delete>
        <configure-table v-else-if="type==='add'" @click="handleClose" :groupId="$route.params.id" />
        <update-form v-else-if="type==='update'" :row_id="edit_id" @click="handleClose" />
      </el-dialog>
      <p class="kylin-page-label">{{ $t("configure.normal_library_configure_table_description") }}</p>
      <ky-table
        :getData="getConfigureList"
        :showSelect="showSelect"
        :search_data="search_data"
        :search_criteria="search_criteria"
        ref="table"
      >
        <template v-slot:table-action>
          <authorization-button class="kylin-item-button" @click="handleAdd" name="config:create">
            {{ $t("add") }}
          </authorization-button>
          <authorization-button
            class="kylin-delete"
            @click="handleDelete"
            name="config:delete"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          >
            {{ $t("delete") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline>
                <el-form-item :label="$t('description')">
                  <span>{{ props.row.description }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="name" :label="$t('name')"> </el-table-column>
          <el-table-column
            prop="configType"
            :label="$t('configure.configure_type')"
          >
          </el-table-column>
          <el-table-column prop="attribute" :label="$t('attribute')">
          </el-table-column>
          <el-table-column
            prop="baseline"
            :label="$t('configure.configure_value')"
          >
          </el-table-column>
          <el-table-column prop="flag" :label="$t('configure.restart_sign')">
          </el-table-column>
          <el-table-column prop="filePath" :label="$t('configure.file_path')">
          </el-table-column>
          <el-table-column>
            <template slot="header">
              {{ $t("action") }}
            </template>
            <template slot-scope="scope">
              <authorization-button
                type="text"
                name="config:update"
                size="medium"
                @click="handleEdit(scope.row)"
              >
                {{ $t("edit") }}
              </authorization-button>
            </template>
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import { getClassInfo } from "@/api/config";
import { getConfigureList, deleteConfigure } from "@/api/configure_group";
import Delete from "@/components/Delete";
import ConfigureTable from "./ConfigureTable.vue";
import UpdateForm from "./UpdateForm.vue";
import AuthorizationButton from "@/components/AuthorizationButton";
export default {
  components: {
    Page,
    kyTable,
    AuthorizationButton,
    Delete,
    ConfigureTable,
    UpdateForm
  },
  data() {
    return {
      showSelect: true,
      title: "",
      display: false,
      edit_id: null,
      delete_content: "",
      type: "",
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
        configGroupId : this.$route.params.id,
      },
      search_data: {
        name: {
          type: "text",
          name: "name",
        },
        configType: {
          name: "configure.configure_type",
          type: "select",
          items: [],
          func: this.getConfigureInfo,
        },
        attribute: {
          name: "attribute",
          type: "select",
          items: [],
        },
        baseline: {
          name: "configure.configure_value",
          type: "text",
        },
        flag: {
          name: "configure.restart_sign",
          type: "select",
          items: [
            { key: true, name: this.$t("true") },
            { key: false, name: this.$t("false") },
          ],
        },
        filePath: {
          name: "configure.file_path",
          type: "select",
          items: [],
        },
      },
    };
  },
  methods: {
    getConfigureList,
    handleEdit(row) {
      this.display = true;
      this.title = this.$t("configure.edit_configure_item");
      this.edit_id = row.id;
      this.type = "update";
    },
    handleClose(type) {
      this.display = false;
      this.edit_id = null;
      (this.title = ""), (this.delete_content = "");
      this.type = "";
      if (type === "success") {
        this.$refs.table.refresh();
      }
    },
    handleDelete() {
      this.display = true;
      this.title = this.$t("configure.delete_configure");
      this.type = "delete";
      let arr = [];
      this.$refs.table.selectRow.rows.map(function (row) {
        arr.push(row.name);
      });
      this.delete_content = this.$t(
        "configure.delete_configure_content"
      ).replace("%s", arr.toString());
    },
    handleAdd() {
      this.display = true;
      this.title = this.$t("configure.add_configure_item");
      this.type = "add";
    },
    async handleDeleteOk() {
      let data = {"groupId": this.$route.params.id};
      if (this.$refs.table.select_id === "2") {
        const res = await getConfigureList({
          ...this.$route.query,
          ...this.search_criteria,
          paged: false,
        });
        let ids = [];
        res.data.data.results.map((item) => {
          if (!this.$refs.table.selectRow.excludeIds.includes(item.id)) {
              ids.push(item.id);
          }
        });
        data["configIds"] = ids;
      } else {
        data["configIds"] = this.$refs.table.selectRow.ids;
      }
      const res = await deleteConfigure(data);
      if (res.data.code == "0") {
        this.$message({
          type: "success",
          message: res.data.message,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.message,
        });
      }
      this.display = false;
      this.edit_id = null;
      this.type = "";
      this.$refs.table.refresh();
    },
    getConfigureInfo() {
      getClassInfo().then((res) => {
        let data = res.data.data;
        let configTypes = Object.keys(data);
        let files = [];
        let attrs = [];
        configTypes.forEach((key) => {
          this.search_data["configType"].items.push({ key, name: key });
          data[key].forEach((item) => {
            for (let k in item) {
              const attrIndex = attrs.indexOf(k);
              if (attrIndex < 0) {
                attrs.push(k);
                this.search_data["attribute"].items.push({ key: k, name: k });
              }
              const index = files.indexOf(item[k]);
              if (index < 0) {
                files.push(item[k]);
                this.search_data["filePath"].items.push({
                  key: item[k],
                  name: item[k],
                });
              }
            }
          });
        });
      });
    },
  },
};
</script>

<style>
</style>